﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using FrameWork.Lib;
using FrameWork.WinUI.Common;

namespace AnimeWatchMax
{
    public partial class frmTitle : DockContent
    {
        public frmTitle()
        {
            InitializeComponent();

            mListViewSorterEpisodes = new FrameWork.WinUI.Common.ListViewSorter(lstEpisodes);
            mListViewSorterTorrents = new FrameWork.WinUI.Common.ListViewSorter(lstTorrents);
            mListViewSorterURLList = new FrameWork.WinUI.Common.ListViewSorter(lstURLList);
        }

        private FrameWork.WinUI.Common.ListViewSorter mListViewSorterEpisodes = null;
        private FrameWork.WinUI.Common.ListViewSorter mListViewSorterTorrents = null;
        private FrameWork.WinUI.Common.ListViewSorter mListViewSorterURLList = null;

        public UIManager UIManager = null;
        public Guid Key;
        public DataObjects.Title Title = null;

        public void View(DataObjects.Title pTitle)
        {
            this.Title = pTitle;
            lstEpisodes.Items.Clear();
            lstTorrents.Items.Clear();
            lstURLList.Items.Clear();
            UpdateViewer();
        }

        public void UpdateViewer()
        {
            txtCreated.Text = Title.TitleCreatedDate.StringValueOrEmpty();
            txtReleased.Text = Title.ReleasedDate.StringValueOrEmpty();
            txtStatus.Text = Title.Status.StringValueOrEmpty();
            txtTitle.Text = Title.Name.StringValueOrEmpty();


            foreach (var tEpisode in Title.EpisodeList)
            {
                var tkey = tEpisode.DataObjectGUID.ToString();

                ListViewItem tItem = null;

                if (!lstEpisodes.Items.ContainsKey(tkey))
                {
                    tItem = lstEpisodes.Items.Add(tkey, tEpisode.Order, 0);
                    tItem.SubItems.Add(tEpisode.State.ToString());
                    tItem.SubItems.Add(tEpisode.Status.ToString());
                    tItem.SubItems.Add(tEpisode.Created.StringValueOrEmpty("MM/dd/yyyy HH:mm"));
                }
                else
                {
                    tItem = lstEpisodes.Items[tkey];
                    tItem.SubItems[1].Text = tEpisode.Order;
                    tItem.SubItems[1].Text = tEpisode.State.ToString();
                    tItem.SubItems[2].Text = tEpisode.Status.ToString();
                    tItem.SubItems[3].Text = tEpisode.Created.StringValueOrEmpty("MM/dd/yyyy HH:mm");
                }

                tItem.Tag = tEpisode.DataObjectGUID;
            }


        }

        private void lstEpisodes_SelectedIndexChanged(object sender, EventArgs e)
        {

            var tSelectedDataObject = lstEpisodes.SelectedDataObjectByGuid(this.Title.EpisodeList.ToList<FrameWork.Lib.DataObjects.DataObjectBase>());
            if (tSelectedDataObject != null)
            {
                ViewTorrents((DataObjects.Episode)tSelectedDataObject);
            }

        }

        public void ViewTorrents(DataObjects.Episode pSelectedEpisode)
        {
            lstTorrents.Items.Clear();

            foreach (var tFile in pSelectedEpisode.FileList)
            {
                var tGroup = lstTorrents.Groups.Add(tFile.DataObjectGUID.ToString(), tFile.Name);
                tGroup.Tag = tFile;
                foreach (var tTorrent in tFile.TorrentList)
                {
                    var tItem = lstTorrents.Items.Add(tTorrent.DataObjectGUID.ToString(), tTorrent.Site, -1);
                    tItem.Group = tGroup;
                    tItem.Tag = tTorrent;
                    tItem.SubItems.Add(System.Web.HttpUtility.UrlDecode(tTorrent.Direct));
                    tItem.SubItems.Add(tTorrent.State.ToString());
                    tItem.SubItems.Add(tTorrent.Status.ToString());

                    tItem.SubItems.Add(tTorrent.UL.ToString());
                    tItem.SubItems.Add(tTorrent.DL.ToString());
                    tItem.SubItems.Add(tTorrent.Downloaded.ToString());

                    if (tTorrent.TrackerProblem || tTorrent.Status == TorrentStatusType.InActive)
                    {
                        tItem.ForeColor = Color.White;
                        tItem.Font = new Font(tItem.Font, FontStyle.Bold);
                        tItem.BackColor = Color.OrangeRed;

                    }
                }
            }
        }

        private void lstTorrents_DoubleClick(object sender, EventArgs e)
        {
            //var tSelectedFile = this.lstTorrents.SelectedGroupDataObject<DataObjects.File>();
            var tSelectedTorrent = this.lstTorrents.SelectedDataObject<DataObjects.Torrent>();


            //var tRequest = System.Net.HttpWebRequest.Create(tSelectedTorrent.Direct);

            //using (var tResponse = tRequest.GetResponse())
            //{
            //    using (var sr = new System.IO.StreamReader(tResponse.GetResponseStream()))
            //    {
            //        var t = sr.ReadToEnd();

            //    }
            //}

            //var test = FrameWork.Lib.Net.Functions.GetSource(tSelectedTorrent.Direct);


            System.Diagnostics.Process.Start(tSelectedTorrent.Direct);
        }

    }
}
